package com.imooc.bilibili.service.impl;

import com.imooc.bilibili.dao.CollectionGroupInfoDao;
import com.imooc.bilibili.dao.VideoInfoDao;
import com.imooc.bilibili.entity.CollectionGroupInfo;
import com.imooc.bilibili.entity.VideoInfo;
import com.imooc.bilibili.enums.GroupTypeEnums;
import com.imooc.bilibili.exception.ConditionException;
import com.imooc.bilibili.request.VideoCollectionRequest;
import com.imooc.bilibili.service.VideoCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;

@Service("videoCollectionService")
public class VideoCollectionServiceImpl implements VideoCollectionService {

    @Autowired
    private CollectionGroupInfoDao collectionGroupInfoDao;

    @Autowired
    private VideoInfoDao videoInfoDao;

    @Override
    public void insertCollectonGroupService(VideoCollectionRequest request) {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        //获取分组信息id
        Long groupId = request.getCollectionGroupId();
        if (null == groupId) {
            //分组信息不在，创建分组信息

        } else {
            //根据参数中的分组id查询分组信息
            CollectionGroupInfo groupInfo = collectionGroupInfoDao.queryById(groupId);
            if (null == groupInfo) {
                throw new ConditionException("分组信息不存在");
            }
        }
        //判断用户收藏的视频信息存在
        Long videoId = request.getVideoId();
        VideoInfo videoInfo = videoInfoDao.queryVideoInfoById(videoId);
        if (null == videoInfo) {
            throw new ConditionException("视频信息不存在");
        }
        CollectionGroupInfo collectionGroupInfo = new CollectionGroupInfo();
        collectionGroupInfo.setGroupType(GroupTypeEnums.DEFAULT_GROUP.getCode());
        collectionGroupInfo.setEnableState(0);
        collectionGroupInfo.setDeleteState(0);
        collectionGroupInfo.setCreateTime(timestamp);
        collectionGroupInfo.setUpdateTime(timestamp);
        collectionGroupInfoDao.insert(collectionGroupInfo);
    }
}
